/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getOwnedOperations;

import java.util.Collections;
import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.VisibilityKind;

/**
 * Command to find an operation with a given name.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Context
 * <li>Name
 * </ul>
 * 
 * <p>
 * The following attributes can be read to access the result of the command:
 * </p>
 * <ul>
 * <li>Operation - will be null if not found.
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class FindOperationCommand extends Command {

	private Namespace context;
	private String name;
	private Operation operation;

	public FindOperationCommand() {
	}

	public FindOperationCommand(final Namespace context, final String name) {
		setContext(context);
		setName(name);
	}

	public void setContext(final Namespace context) {
		this.context = context;
	}

	public void setName(final String name) {
		this.name = (name != null) ? name.trim() : null;
	}

	public Operation getOperation() {
		return operation;
	}

	@Override
	public void execute() {
		if (name == null || name.length() == 0)
			return; // nothing to do, keep result == null

		operation = search(context, true);
		if (operation != null)
			return;

		Namespace searchContext = (Namespace) context.getOwner();
		while (searchContext != null) {
			for (Object member : searchContext.getOwnedMembers()) {
				if (member instanceof Namespace) {
					operation = search((Namespace) member, false);
					if (operation != null)
						return;
				}
			}
			searchContext = (Namespace) searchContext.getOwner();
		}
	}

	private Operation search(final Namespace scope, final boolean allowPrivate) {
		for (Operation candidate : internalGetOwnedOperations(scope)) {
			if (isMatch(candidate, allowPrivate))
				return candidate;
		}
		return null;
	}

	private List<Operation> internalGetOwnedOperations(Namespace owner) {
		if (owner instanceof org.eclipse.uml2.uml.Class)
			return getOwnedOperations((org.eclipse.uml2.uml.Class) owner);
		else if (owner instanceof org.eclipse.uml2.uml.Interface)
			return getOwnedOperations((org.eclipse.uml2.uml.Interface) owner);
		else
			return Collections.emptyList();
	}

	private boolean isMatch(final Operation operation,
			final boolean allowPrivate) {
		return (name.equalsIgnoreCase(operation.getName()) && (allowPrivate || operation
				.getVisibility() != VisibilityKind.PRIVATE_LITERAL));
	}

	@Override
	public void undo() {
		operation = null;
	}

	@Override
	public void redo() {
		this.execute();
	}

	@Override
	public void dispose() {
		context = null;
		name = null;
		operation = null;
	}
}
